package com.cn.delan.web.mvc.admin.action;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.cn.delan.core.util.StringUtil;
import com.cn.delan.web.mvc.admin.AdminConst;
import com.cn.delan.web.mvc.admin.bean.UserBean;
import com.cn.delan.web.mvc.admin.service.IUserService;
import com.cn.delan.web.mvc.core.action.AbstractAction;
/**
 * 编辑用户
 * @author BingGo
 *
 */
public class EditUserAction extends AbstractAction {

	private IUserService<UserBean> userService;
	
	public IUserService<UserBean> getUserService() {
		return userService;
	}
	public void setUserService(IUserService<UserBean> userService) {
		this.userService = userService;
	}

	@Override
	public ModelAndView handle(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String oid = this.getParameter("_oid");
		String id = this.getParameter("id");
		String userid = this.getParameter("userid");
		String password = this.getParameter("password");
		String username = this.getParameter("username");
		String roleid = this.getParameter("roleid");
		String superman = this.getParameter("superman");
		String lock = this.getParameter("lock");
		if(StringUtil.isEmpty(superman)){
			superman = "No";
		}
		if(StringUtil.isEmpty(lock)){
			lock = "No";
		}
		if(!StringUtil.isEmpty(oid)){
			UserBean user = (UserBean)this.userService.findById(oid);
			request.setAttribute(AdminConst.REQUEST_ATTR_EDITOBJECT, user);
			//显示编辑页面
			return this.getView(INPUT);
		}else{
			//进行保存操作
			PrintWriter out = this.getWriter();
			if(!this.userService.exists(userid)){
				//用户不存在
				out.print(this.getMessage(AdminConst.MESSAGE_USER_NOTEXISTS));
			}else{
				try{
					if(this.userService.update(id,password,username,roleid,superman,lock)){
						//编辑成功
						out.print(this.getMessage(AdminConst.MESSAGE_USER_EDITSUCCESS));
					}else{
						//编辑失败
						out.print(this.getMessage(AdminConst.MESSAGE_USER_EDITFAIL));
					}
				}catch(Exception e){
					//编辑失败
					out.print(this.getMessage(AdminConst.MESSAGE_USER_EDITFAIL));
				}
			}
			return null;
		}
	}

}
